<?php

use yii\helpers\Url;
use backend\components\helper\EasyHtml;
use backend\components\widget\adminLTE\GridView;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\search\AppModulesSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

\backend\components\widget\file\UploadAsset::register($this);

$this->title = '应用模块';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-header with-border">
                <h3 class="box-title"><?= EasyHtml::encode($this->title) ?></h3>
                <div class="box-tools pull-right">
                    <?= EasyHtml::iconLink('导入', null, [
                        'id' => 'import',
                        'icon' => 'cloud-upload',
                        'class' => 'btn btn-primary',
                    ]) ?>
                    <?= EasyHtml::iconLink('新增', ['create'], [
                        'icon' => 'plus',
                        'class' => 'btn btn-success'
                    ]) ?>
                </div>
            </div>
            <div class="box-tools form-inline">
                <?php echo $this->render('_search', ['model' => $searchModel]); ?>
                <div class="box-body">
                    <?= GridView::widget([
                        'dataProvider' => $dataProvider,
                        'columns'      => [
                            ['class' => 'yii\grid\SerialColumn', 'header' => '序号'],
                            'id',
                            'name',
                            [
                                'attribute' => 'type',
                                'value' => function($model){
                                    return $model->typeTexts[$model->type]??'';
                                }
                            ],
                            'class',
                            //'config',
                            [
                                'label' => '启用',
                                'class' => \nickdenry\grid\toggle\components\RoundSwitchColumn::class,
                                'attribute' => 'status',
                                'headerOptions' => ['width' => 150],//列长
                            ],
                            'created_at',
                            [
                                'class' => 'backend\components\widget\adminLTE\ActionColumn',
                                'template' => '{update} {delete}',
                            ],
                        ],
                    ]); ?>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="file-div-box" class="hide">
    <div class="file-upload-tools"></div>
    <input type="file" style="display: none" accept="application/zip">
</div>
<script>
    $(function () {
        (new Upload()).init({
            uploadEle:'#import',
            mimeTypes:['application/x-zip-compressed','application/zip'],
            uploadUrl:"<?=Url::to('/file/upload');?>",
            uploadPath:'import',
            uploadSuccess:function (res,Upload) {
                layer.confirm('是否立即导入扩展?',{
                    btn: ['导入']
                }, function(){
                    $.post('<?=Url::to(["import"]);?>',{zipFile:res.data.absolutePath},function (res) {
                        if(res.code){
                            layer.msg(res.message,{time: 1000}, function(){
                                window.location.reload();
                            });
                        }else{
                            layer.confirm(res.message,{btn:'确定'});
                        }
                    });
                });
            }
        }).click();
    });
</script>
